package com.t216.dm216.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.t216.dm216.common.PageUtil;
import com.t216.dm216.mapper.DmSchedulerMapper;
import com.t216.dm216.pojo.DmCinema;
import com.t216.dm216.pojo.DmItem;
import com.t216.dm216.pojo.DmScheduler;
import com.t216.dm216.pojo.dto.DmItemDTO;
import com.t216.dm216.pojo.dto.DmSchedulerSearchDTO;
import com.t216.dm216.pojo.dto.SearchParamDTO;
import com.t216.dm216.service.DmSchedulerService;
import com.t216.dm216.wrapper.DmMPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
@Service
public class DmSchedulerServiceImpl implements DmSchedulerService {
    @Resource
    private DmSchedulerMapper dmSchedulerMapper;
    @Override
    public PageUtil<DmSchedulerSearchDTO> getSearchItem(SearchParamDTO searchParamDTO) {
        DmMPJLambdaWrapper<DmScheduler> lambdaWrapper = new DmMPJLambdaWrapper<>(DmScheduler.class);
        /*要查询的信息*/
        lambdaWrapper.select( DmScheduler::getStartTime, DmScheduler::getEndTime);
        lambdaWrapper.select(DmCinema::getAreaName, DmCinema::getAddress, DmCinema::getLatitude, DmCinema::getLongitude);
        lambdaWrapper.select(DmItem::getItemName, DmItem::getAbstractMessage, DmItem::getMinPrice,DmItem::getMaxPrice);
        lambdaWrapper.selectAs(DmItem::getId,DmScheduler::getId);
        lambdaWrapper.leftJoin(DmCinema.class,DmCinema::getId,DmScheduler::getCinemaId);
        lambdaWrapper.leftJoin(DmItem.class,DmItem::getId,DmScheduler::getItemId);

        /*查询条件*/
        /*上策: 1:自己封装MPJLambdaWrapper 2:使用实体类的属性来进行验证 */
        /*中策:自己写空值判断*/
        /*下策:给他生成Mapper接口*/
        /*只验证null*/
        lambdaWrapper.eq(searchParamDTO.getItemTypeId1() != null && searchParamDTO.getItemTypeId1() != 0,DmItem::getItemType1Id,searchParamDTO.getItemTypeId1());
        lambdaWrapper.eqIfExists(DmItem::getItemType1Id,searchParamDTO.getItemTypeId1());
        lambdaWrapper.eqIfExists(DmItem::getItemType2Id,searchParamDTO.getItemTypeId2());
        lambdaWrapper.eqIfExists(DmCinema::getAreaId,searchParamDTO.getAreaId());
        lambdaWrapper.likeIfExists(DmItem::getItemName,searchParamDTO.getKeyword());
        /*gt:大于 e:equals*/
        lambdaWrapper.geIfExists(DmScheduler::getStartTime,searchParamDTO.getStartTime());
        lambdaWrapper.leIfExists(DmScheduler::getEndTime,searchParamDTO.getEndTime());

        /*分页的信息*/
        Page<DmSchedulerSearchDTO> page = new Page<>(searchParamDTO.getCurrentPage(), searchParamDTO.getPageSize());

        PageUtil<DmSchedulerSearchDTO> pageUtil = new PageUtil<>(dmSchedulerMapper.selectJoinPage(page,DmSchedulerSearchDTO.class,lambdaWrapper));
        return pageUtil;
    }

    @Override
    public List<DmScheduler> getSchedulerByItemId(Long itemId) {
        LambdaQueryWrapper<DmScheduler> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.eq(DmScheduler::getItemId,itemId);
        return dmSchedulerMapper.selectList(lambdaWrapper);
    }
}
